/**
 * Laboratory:云计算中心
 * Author:吕国伟
 */
module.exports = {
    //计数器-例如：阅读数-点赞数等
    getNumber(num) {
        if (num < 1000) {
            return num
        } else if (num >= 1000 && num < 10000) {
            let n = (num / 1000).toFixed(1) + "k"
            return n
        } else if (num >= 10000 && num < 100000) {
            let n = (num / 10000).toFixed(1) + "w"
            return n
        } else if (num >= 100000) {
            return "10w+"
        } else {
            return 0
        }
    },
    //日期转化-时间戳转化时间格式方法
    getTime(t, type = 0) {
        let time = new Date(t);
        let year = time.getFullYear() + "";
        let month = time.getMonth() + 1;
        month = month < 10 ? "0" + month : month + "";
        let day = time.getDate();
        day = day < 10 ? "0" + day : day + "";
        let hour = time.getHours();
        hour = hour < 10 ? "0" + hour : hour + "";
        let min = time.getMinutes();
        min = min < 10 ? "0" + min : min + "";
        let second = time.getSeconds();
        second = second < 10 ? "0" + second : second + "";
        let arr = [
            `${year}-${month}-${day}`,
            `${year}年${month}月${day}日`,
            `${year}-${month}-${day} ${hour}:${min}:${second}`,
            `${year}年${month}月${day} ${hour}时${min}分${second}秒`,
            `${month}-${day}`,
            `${month}月${day}日`,
            `${hour}:${min}:${second}`,
            `${hour}时${min}分${second}秒`
        ]
        return arr[type]
    },
    /**
     * 将周数字换位字符串
     */
    changeWeek(week) {
        let num = parseInt(week)
        switch (num) {
            case 1:
                return "周一";
                break;
            case 2:
                return "周二";
                break;
            case 3:
                return "周三";
                break;
            case 4:
                return "周四";
                break;
            case 5:
                return "周五";
                break;
            case 6:
                return "周六";
                break;
            case 7:
                return "周日";
                break;
            default:
                return "后天"
        }
    },

    /**
     * 内容省略模式
     */
    changeDetails(str, num) {
        let number = parseInt(num)
        if(str ){
          if ( number < str.length) {
            let conent = str.slice(0, number) + "..."
            return conent
        } else {
            let conent = str.slice(0, number)
            return conent
        }
        }
        

    },
    // 截取时间
    changeTime(str, num) {
        let number = parseInt(num)
        let conent = str.slice(0, number)
        return conent
    },
    // 分割图片
    splitImg(image) {
        let data = image
        let img = data.split("|")
        return img
    },
    // 我的发布 图片命名 获取当前时间
    nowTime() {
        let time = new Date();
        //let tim = time.toLocaleDateString()
        let year = time.getFullYear()
        let month = time.getMonth() + 1
        let day = time.getDate()
        let t = `/${year}/${month}/${day}`
        return t
    },
    // 字符替换工具
    changeStr(data) {
        let da = data
        let i = 0
        let t = ""
        for (i = 0; i < da.length; i++) {
            t = t.concat(da[i].replace('/', '.'))
        }
        return t
    },
    // enjoy转
    utf16toEntities(str) {
        var patt = /[\ud800-\udbff][\udc00-\udfff]/g; // 检测utf16字符正则  
        return str.replace(patt, function (char) {
            var H, L, code;
            if (char.length === 2) {
                H = char.charCodeAt(0); // 取出高位  
                L = char.charCodeAt(1); // 取出低位  
                code = (H - 0xD800) * 0x400 + 0x10000 + L - 0xDC00; // 转换算法  
                return "&#" + code + ";";
            } else {
                return char;
            }
        });
    },
    // 转enjoy
    entitiesToUtf16(str) {
        return str.replace(/&#(\d+);/g, function (match, dec) {
            let H = Math.floor((dec - 0x10000) / 0x400) + 0xD800;
            let L = Math.floor(dec - 0x10000) % 0x400 + 0xDC00;
            return String.fromCharCode(H, L);
        });
    },
    // 去除标签
    delHtmlTag(str) {
        return str.replace(/<[^>]+>/g, "");
    },
    // 保留三位小数
    float(params) {
      let newpar = parseFloat(params);
      let reg = /^[0-9]+.?[0-9]*$/;
      if (reg.test(newpar)){
          let newNum = newpar.toFixed(3);
          return newNum;
      }
  }
}
